package com.shixi.shixitraining.shixitraining.ui.tabapply.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.TabhostBaseFragment;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabapply.adapter.ApplyTrainingAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabapply.entity.ApplyTrainingBean;
import com.shixi.shixitraining.shixitraining.ui.tabapply.activity.TrainingApplyDetailActivity;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.util.UserManager;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.List;

import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.APPLYTRAINING;

/**
 * Created by shenyan on 2018/7/17.
 */

public class ApplyTrainingFragment extends TabhostBaseFragment {


    private TextView mDataTv;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_training_project;
    }

    @Override
    protected void initPager() {
        mDataTv = (TextView) mContentView.findViewById(R.id.tv_nodata);

        String token = UserManager.getInstance().getToken();
        String userId = UserManager.getInstance().getUserId();
        initTraining(userId,token);

    }

    private void initTraining(String userId,String token) {
        String url=APPLYTRAINING+"&userId="+userId+"&token="+token;

        HttpUtil.getInstance().getApiInterface().applyTraining(url).enqueue(new CallbackImple<ApplyTrainingBean>() {
            @Override
            public void onError(Call<ApplyTrainingBean> call, Throwable t) {
                ToastUtil.showShortToast(getContext(),t.getMessage());
            }
            @Override
            public void onSuccess(Call<ApplyTrainingBean> call, ApplyTrainingBean response) {
                if (response.getData()!=null&&response.getData().size()>0){
                    if (response.getStatus()==200){
                        final List<ApplyTrainingBean.DataBean> data = response.getData();
                        RecyclerView  rcview_training_projct = (RecyclerView) mContentView.findViewById(R.id.rcview_training);
                        LinearLayoutManager manager=new LinearLayoutManager(getContext());
                        rcview_training_projct.setLayoutManager(manager);
                        ApplyTrainingAdapter adapter=new ApplyTrainingAdapter(getContext(),data);
                        rcview_training_projct.setAdapter(adapter);
                        rcview_training_projct.addOnItemTouchListener(new RecycleItemClickListener(getContext()) {
                            @Override
                            public void itemClickListener(View view, int position) {
                                ApplyTrainingBean.DataBean dataBean = data.get(position);
                                String number = dataBean.getNumber();
                                Bundle  bundle=new Bundle();
                                bundle.putString("number",number);
                                Intent intent=new Intent(getContext(),TrainingApplyDetailActivity.class);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });

                    } else {
                        ToastUtil.showShortToast(getContext(), response.getMsg());
                    }
                }else {
                    mDataTv.setVisibility(View.VISIBLE);
                    ToastUtil.showShortToast(getContext(),"数据为空");
                }
            }

        });

    }

}
